﻿using System;
using NPoco;
using System.Text;
using System.Collections.Generic;

namespace SrBBSPro.Services
{
    public class AnalysisService<T>
    {
        protected Database DbBase { get; }

        public AnalysisService()
        {
            DbBase = new Database("constr");
        }


        public List<T> GetAnalysisData(string kind, long Id, string pro)
        {
            var li = new List<T>();
            var sql = new StringBuilder();
            switch (kind)
            {
                case "EveryDay":
                    var str = DbBase.Fetch<string[]>("SELECT MIN(HappenDay), " +
                        "CASE WHEN MAX(ReviseDay) >MAX(HappenDay) THEN MAX(ReviseDay) ELSE MAX(HappenDay) END FROM Bug Where Id=@0 And SUBSTRING(NO,1,2)=@1", Id, pro);
                    if (string.IsNullOrEmpty(str[0][0]) || string.IsNullOrEmpty(str[0][1]))
                        return li;

                    var days = new List<string>();
                    days.Add(str[0][0]);
                    var start = DateTime.Parse(str[0][0]);
                    sql.AppendFormat("SELECT '{0}' BugDay,(SELECT COUNT(*) FROM Bug WHERE Id=@0 AND HappenDay='{0}' And SUBSTRING(NO,1,2)=@1) Happens,"
                        ,start.ToString("yyyy/MM/dd"));
                    sql.AppendFormat("(SELECT COUNT(*) FROM Bug WHERE Id=@0 AND ReviseDay='{0}' And SUBSTRING(NO,1,2)=@1)  Revises "
                        ,start.ToString("yyyy/MM/dd"));
                    while (start.CompareTo(DateTime.Parse(str[0][1])) < 0)
                    {
                        start = start.AddDays(1);
                        sql.AppendFormat(" UNION SELECT '{0}' BugDay,(SELECT COUNT(*) FROM Bug WHERE Id=@0 AND HappenDay='{0}' And SUBSTRING(NO,1,2)=@1) Happens,"
                        ,start.ToString("yyyy/MM/dd"));
                        sql.AppendFormat("(SELECT COUNT(*) FROM Bug WHERE Id=@0 AND ReviseDay='{0}' And SUBSTRING(NO,1,2)=@1) Revises "
                            , start.ToString("yyyy/MM/dd"));
                    }
                    break;
                case "Reviser":
                    sql.Append(" SELECT  P.Reviser,P.ReviserName,");
                    sql.Append(" COUNT(*) Publishs,");
                    sql.Append(" COUNT(B.ConfirmDay) Confirms,");
                    sql.Append(" (COUNT(*)-COUNT(B.ConfirmDay)) NoConfirms");
                    sql.Append(" FROM BUG  B,PageInfo P");
                    sql.Append("  Where B.Id=@0");
                    sql.Append(" And B.ID=P.Id");
                    sql.Append(" AND B.PageId =P.No");
                    sql.Append(" And SUBSTRING(B.NO,1,2)=@1");
                    sql.Append(" GROUP BY P.Reviser,P.ReviserName");
                    break;
                case "BookDay":
                    sql.Append(" SELECT COUNT(*) Publishs, COUNT(ConfirmDay) Confirms,");
                    sql.Append(" BookEndDate,");
                    sql.Append(" (COUNT(*)-COUNT(ConfirmDay)) NoConfirms");
                    sql.Append(" FROM  Bug  Where Id=@0 And SUBSTRING(NO,1,2)=@1 GROUP BY BookEndDate");
                    break;
                case "Tester":
                    sql.Append(" SELECT Issuer as Tester,IssuerName as TesterName, COUNT(*) Publishs");
                    sql.Append(" ,COUNT(ReviseDay) Revises,COUNT(ConfirmDay) Confirms,(COUNT(*)-COUNT(ConfirmDay)) NoConfirms");
                    sql.Append(" FROM  Bug  Where Id=@0 And SUBSTRING(NO,1,2)=@1 GROUP BY Issuer,IssuerName");
                    break;
                case "BugManager":
                    sql.Append("SELECT No PageId,PageName,DeveloperName,TesterName,");
                    sql.Append("(SELECT COUNT(*) FROM  Bug WHERE Id=P.Id AND PageId =P.No And SUBSTRING(NO,1,2)=@1) Publishs, ");
                    sql.Append("(SELECT COUNT(*) FROM  Bug WHERE Id=P.Id AND PageId =P.No And SUBSTRING(NO,1,2)=@1 AND ConfirmDay IS NOT NULL) Confirms,");
                    sql.Append("(SELECT COUNT(*) FROM  Bug WHERE Id=P.Id AND PageId =P.No And SUBSTRING(NO,1,2)=@1 AND ConfirmDay IS NULL AND ReviseDay IS NOT NULL) NoConfirms,");
                    sql.Append("(SELECT COUNT(*) FROM  Bug WHERE Id=P.Id AND PageId =P.No And SUBSTRING(NO,1,2)=@1 AND BugKind <>'0' ) Bugs");
                    sql.Append(" FROM PageInfo P");
                    sql.Append(" Where Id=@0");
                    break;
                case "ReasonSmall":
                    sql.Append(" SELECT No PageId,PageName");
                    for (int i = 0; i < 21; i++)
                    {
                        sql.AppendFormat(",(SELECT COUNT(*) FROM  Bug WHERE Id=P.Id AND PageId =P.No And ReasonSmall='{0}' And SUBSTRING(NO,1,2)=@1) small{0}", i);
                    }
                    sql.Append(" FROM PageInfo P");
                    sql.Append(" Where Id=@0");
                    break;
                case "ReasonBig":
                    sql.Append(" SELECT No PageId,PageName");
                    for (int i = 0; i < 21; i++)
                    {
                        sql.AppendFormat(",(SELECT COUNT(*) FROM  Bug WHERE Id=P.Id AND PageId =P.No And ReasonBig='{0}' And SUBSTRING(NO,1,2)=@1) big{0}", i);
                    }
                    sql.Append(" FROM PageInfo P");
                    sql.Append(" Where Id=@0");
                    break;
                case "Scence":
                    sql.Append(" SELECT No PageId,PageName");
                    for (int i = 0; i < 21; i++)
                    {
                        sql.AppendFormat(",(SELECT COUNT(*) FROM  Bug WHERE Id=P.Id AND PageId =P.No And SceneKind='{0}' And SUBSTRING(NO,1,2)=@1) scene{0}", i);
                    }
                    sql.Append(" FROM PageInfo P");
                    sql.Append(" Where Id=@0");
                    break;
                case "CodeQuality":
                    sql.Append(" SELECT Tname,Scale,NUM");
                    sql.Append(" ,SUBSTRING(CONVERT(varchar(30),ROUND(NUM/Scale,2)),0,5) Density ");
                    sql.Append(" From(SELECT  P.Id,P.DeveloperName Tname ,SUM(Scale) Scale");
                    sql.Append(" ,(SELECT COUNT(*) FROM  Bug WHERE ID =P.Id  And SUBSTRING(NO,1,2)=@1 AND PageId IN(SELECT No FROM PageInfo WHERE ID=P.Id AND DeveloperName =P.DeveloperName)) Num");
                    sql.Append(" FROM PageInfo P");
                    sql.Append(" Where Id=@0");
                    sql.Append(" GROUP BY P.DeveloperName,P.Id");
                    sql.Append(" )Sub");
                    break;
                case "TestQuality":
                    sql.Append(" SELECT Tname,Scale,NUM");
                    sql.Append(" ,SUBSTRING(CONVERT(varchar(30),ROUND(NUM/Scale,2)),0,5) Density ");
                    sql.Append(" From(SELECT  P.Id,P.TesterName Tname ,SUM(Scale) Scale");
                    sql.Append(" ,(SELECT COUNT(*) FROM  Bug WHERE ID =P.Id  And SUBSTRING(NO,1,2)=@1 AND PageId IN(SELECT No FROM PageInfo WHERE ID=P.Id AND TesterName =P.TesterName)) Num");
                    sql.Append(" FROM PageInfo P");
                    sql.Append(" Where Id=@0");
                    sql.Append(" GROUP BY P.TesterName,P.Id");
                    sql.Append(" )Sub");
                    break;
                case "SysQuality":
                    sql.Append(" SELECT Tname,Scale,NUM");
                    sql.Append(" ,SUBSTRING(CONVERT(varchar(30),ROUND(NUM/Scale,2)),0,5) Density ");
                    sql.Append(" From(SELECT  P.No Tname,P.Scale");
                    sql.Append(",(SELECT COUNT(*) FROM  Bug WHERE ID=P.Id AND PageId=P.No And SUBSTRING(NO,1,2)=@1 ) Num");
                    sql.Append(" FROM PageInfo P");
                    sql.Append(" Where Id=@0) Sub");
                    break;
            };
            li = DbBase.Fetch<T>(sql.ToString(), Id, pro);
            return li;
        }

    }
}